package com.yanqu.road.entity.activity.feast.data;

import com.yanqu.road.entity.DataObject;
import com.yanqu.road.entity.DataStatus;
import com.yanqu.road.entity.FieldTypeAnnotation;
import com.yanqu.road.entity.activity.feast.pvp.Coordinate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@FieldTypeAnnotation(tableName = "t_c_feast_pvp_user_data")
public class CrossFeastPvpUserData extends DataObject {

    @FieldTypeAnnotation(desc = "活动ID", select = true, update = true)
    private int activityId;

    @FieldTypeAnnotation(desc = "玩家ID", update = true)
    private long userId;

    @FieldTypeAnnotation(desc = "玩家pve属性，及时变更")
    private FeastPveUserProperty pveProperty = new FeastPveUserProperty();

    @FieldTypeAnnotation(desc = "玩家pvp属性,生成/复活时就固定了")
    private FeastPvpUserProperty pvpProperty = new FeastPvpUserProperty();

    @FieldTypeAnnotation(desc = "所在地点;层级id")
    private int placeId;

    @FieldTypeAnnotation(desc = "位置信息")
    private CrossFeastPvpPosition position = new CrossFeastPvpPosition();

    @FieldTypeAnnotation(desc = "可传送的时间;有CD")
    private long transferTime;

    @FieldTypeAnnotation(desc = "最后一次攻击时间")
    private long attackTime;

    @FieldTypeAnnotation(desc = "最后一次被攻击时间")
    private long attackedTime;

    @FieldTypeAnnotation(desc = "复活时间")
    private long reviveTime;

    @FieldTypeAnnotation(desc = "晋级时间")
    private long upDemandTime;

    @FieldTypeAnnotation(desc = "降级时间")
    private long downDemandTime;

    @FieldTypeAnnotation(desc = "锁定的玩家")
    private CrossFeastPvpLockTarget lockTarget = CrossFeastPvpLockTarget.EMPTY;

    @FieldTypeAnnotation(desc = "复活次数：当前层")
    private int reviveCnt;

    @FieldTypeAnnotation(desc = "击杀次数：当前层")
    private int killCnt;

    @FieldTypeAnnotation(desc = "无敌时间")
    private long protectTime;

    @FieldTypeAnnotation(desc = "属性更新时间")
    private long updatePropertyTime;

    @FieldTypeAnnotation(desc = "体力使用总数量")
    private int totalUseEnergy;

    @FieldTypeAnnotation(desc = "更新体力总数量时间")
    private long updateEnergyTime;

    @FieldTypeAnnotation(desc = "本条命战绩")
    private CrossFeastPvpBattleRecord battleRecord = new CrossFeastPvpBattleRecord();

    @FieldTypeAnnotation(desc = "历史轮次战绩")
    private Map<Integer, CrossFeastPvpBattleRecord> roundBattleRecordMap = new ConcurrentHashMap<>();

    @FieldTypeAnnotation(desc = "个人击杀总战绩")
    private long attackScore;

    @FieldTypeAnnotation(desc = "个人占点总战绩")
    private long seizeScore;

    @FieldTypeAnnotation(desc = "个人战绩更新时间")
    private long updateScoreTime;

    @FieldTypeAnnotation(desc = "房间心跳时间")
    private long lastPingPlaceTime;

    @FieldTypeAnnotation(desc = "总击杀次数")
    private long totalKillCnt;

    @FieldTypeAnnotation(desc = "是否有资格参与pvp")
    private boolean pvpAble;

    @FieldTypeAnnotation(desc = "报名状态")
    private boolean apply;

    @FieldTypeAnnotation(desc = "首次进入")
    private List<Integer> enterPlaceList = new ArrayList<>();

    //头像半径
    private int profileRadius;

    //加速结束时间
    private long speedEndTime;
    //上次加速时间
    private long lastSpeedTime;

    public CrossFeastPvpUserData() {

    }

    public int getActivityId() {
        return activityId;
    }

    public void setActivityId(int activityId) {
        if (this.activityId != activityId) {
            this.activityId = activityId;
            setOp(DataStatus.Update);
        }
    }

    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            setOp(DataStatus.Update);
        }
    }

    public CrossFeastPvpBattleRecord getBattleRecord() {
        return battleRecord;
    }

    public void setBattleRecord(CrossFeastPvpBattleRecord battleRecord) {
        if (battleRecord == null) {
            return;
        }
        this.battleRecord = battleRecord;
        setOp(DataStatus.Update);
    }

    public Map<Integer, CrossFeastPvpBattleRecord> getRoundBattleRecordMap() {
        return roundBattleRecordMap;
    }

    public void setRoundBattleRecordMap(Map<Integer, CrossFeastPvpBattleRecord> roundBattleRecordMap) {
        if (roundBattleRecordMap == null) {
            return;
        }
        this.roundBattleRecordMap = roundBattleRecordMap;
        setOp(DataStatus.Update);
    }

    public FeastPveUserProperty getPveProperty() {
        return pveProperty;
    }

    public void setPveProperty(FeastPveUserProperty pveProperty) {
        if (null == pveProperty) {
            return;
        }
        this.pveProperty = pveProperty;
        setOp(DataStatus.Update);
        // 更新属性时间
        setUpdatePropertyTime(System.currentTimeMillis());
    }

    public FeastPvpUserProperty getPvpProperty() {
        return pvpProperty;
    }

    public void setPvpProperty(FeastPvpUserProperty pvpProperty) {
        if (null == pvpProperty) {
            return;
        }
        this.pvpProperty = pvpProperty;
        setOp(DataStatus.Update);
    }

    public int getPlaceId() {
        return placeId;
    }

    public void setPlaceId(int placeId) {
        if (this.placeId != placeId) {
            this.placeId = placeId;
            setOp(DataStatus.Update);
        }
    }

    public CrossFeastPvpPosition getPosition() {
        return position;
    }

    public void setPosition(CrossFeastPvpPosition position) {
        if (null == position) {
            return;
        }
        this.position = position;
        setOp(DataStatus.Update);
    }

    public long getTransferTime() {
        return transferTime;
    }

    public void setTransferTime(long transferTime) {
        if (this.transferTime != transferTime) {
            this.transferTime = transferTime;
            setOp(DataStatus.Update);
        }
    }

    public long getAttackTime() {
        return attackTime;
    }

    public void setAttackTime(long attackTime) {
        if (this.attackTime != attackTime) {
            this.attackTime = attackTime;
            setOp(DataStatus.Update);
        }
    }

    public long getAttackedTime() {
        return attackedTime;
    }

    public void setAttackedTime(long attackedTime) {
        if (this.attackedTime != attackedTime) {
            this.attackedTime = attackedTime;
            setOp(DataStatus.Update);
        }
    }

    public long getReviveTime() {
        return reviveTime;
    }

    public void setReviveTime(long reviveTime) {
        if (this.reviveTime != reviveTime) {
            this.reviveTime = reviveTime;
            setOp(DataStatus.Update);
        }
    }

    public long getUpdatePropertyTime() {
        return updatePropertyTime;
    }

    public void setUpdatePropertyTime(long updatePropertyTime) {
        if (this.updatePropertyTime != updatePropertyTime) {
            this.updatePropertyTime = updatePropertyTime;
            setOp(DataStatus.Update);
        }
    }

    public int getReviveCnt() {
        return reviveCnt;
    }

    public void setReviveCnt(int reviveCnt) {
        if (this.reviveCnt != reviveCnt) {
            this.reviveCnt = reviveCnt;
            setOp(DataStatus.Update);
        }
    }

    public long getAttackScore() {
        return attackScore;
    }

    public void setAttackScore(long attackScore) {
        if (this.attackScore != attackScore) {
            this.attackScore = attackScore;
            setOp(DataStatus.Update);
        }
    }

    public long getSeizeScore() {
        return seizeScore;
    }

    public void setSeizeScore(long seizeScore) {
        if (this.seizeScore != seizeScore) {
            this.seizeScore = seizeScore;
            setOp(DataStatus.Update);
        }
    }

    public long getUpdateScoreTime() {
        return updateScoreTime;
    }

    public void setUpdateScoreTime(long updateScoreTime) {
        if (this.updateScoreTime != updateScoreTime) {
            this.updateScoreTime = updateScoreTime;
            setOp(DataStatus.Update);
        }
    }

    public long getLastPingPlaceTime() {
        return lastPingPlaceTime;
    }

    public void setLastPingPlaceTime(long lastPingPlaceTime) {
        if (this.lastPingPlaceTime != lastPingPlaceTime) {
            this.lastPingPlaceTime = lastPingPlaceTime;
            setOp(DataStatus.Update);
        }
    }

    /**
     * 获取用户状态  0 正常，1 中间状态
     */
    public int getUserState() {
        return pvpProperty.getHp() <= 0 || reviveTime > System.currentTimeMillis() || upDemandTime > System.currentTimeMillis() || downDemandTime > System.currentTimeMillis() ? 1 : 0;
    }

    /**
     * 是否可移动
     */
    public boolean canMoveAble(){
        return !(pvpProperty.getHp() <= 0 || reviveTime > System.currentTimeMillis());
    }

    public boolean canBattle(){
        return isLive() && !isProtectTime();
    }

    public long getUpdateEnergyTime() {
        return updateEnergyTime;
    }

    public void setUpdateEnergyTime(long updateEnergyTime) {
        if (this.updateEnergyTime != updateEnergyTime) {
            this.updateEnergyTime = updateEnergyTime;
            setOp(DataStatus.Update);
        }
    }

    public int getTotalUseEnergy() {
        return totalUseEnergy;
    }

    public void setTotalUseEnergy(int totalUseEnergy) {
        if (this.totalUseEnergy != totalUseEnergy) {
            this.totalUseEnergy = totalUseEnergy;
            setOp(DataStatus.Update);
            // 更新时间
            setUpdateEnergyTime(System.currentTimeMillis());
        }
    }
    public boolean isPvpAble() {
        return pvpAble;
    }

    public void setPvpAble(boolean pvpAble) {
        if (this.pvpAble != pvpAble) {
            this.pvpAble = pvpAble;
            setOp(DataStatus.Update);
        }
    }

    public boolean isApply() {
        return apply;
    }

    public void setApply(boolean apply) {
        if(this.apply != apply) {
            this.apply = apply;
            setUpdateOption();
        }
    }

    public CrossFeastPvpLockTarget getLockTarget() {
        return lockTarget;
    }

    public void setLockTarget(CrossFeastPvpLockTarget lockTarget) {
        this.lockTarget = lockTarget;
        setUpdateOption();
    }

    public long getProtectTime() {
        return protectTime;
    }

    public void setProtectTime(long protectTime) {
        if(this.protectTime != protectTime) {
            this.protectTime = protectTime;
            setUpdateOption();
        }
    }

    public int getCampId(){
        return 0;
    }


    public long getUpDemandTime() {
        return upDemandTime;
    }

    public void setUpDemandTime(long upDemandTime) {
        if(this.upDemandTime != upDemandTime) {
            this.upDemandTime = upDemandTime;
            setUpdateOption();
        }
    }

    public long getDownDemandTime() {
        return downDemandTime;
    }

    public void setDownDemandTime(long downDemandTime) {
        if(this.downDemandTime != downDemandTime) {
            this.downDemandTime = downDemandTime;
            setUpdateOption();
        }
    }

    public int getKillCnt() {
        return killCnt;
    }

    public void setKillCnt(int killCnt) {
        if(this.killCnt != killCnt) {
            this.killCnt = killCnt;
            setUpdateOption();
        }
    }

    public long getTotalKillCnt() {
        return totalKillCnt;
    }

    public void setTotalKillCnt(long totalKillCnt) {
        if(this.totalKillCnt != totalKillCnt) {
            this.totalKillCnt = totalKillCnt;
            setUpdateOption();
        }
    }

    public List<Integer> getEnterPlaceList() {
        return enterPlaceList;
    }

    public void setEnterPlaceList(List<Integer> enterPlaceList) {
        this.enterPlaceList = enterPlaceList;
        setUpdateOption();
    }

    /**
     * 晋级/降级状态（单靠时间来判断是不行的）
     */
    private boolean upDownDemand;

    /**
     * 存活状态
     */
    public boolean isLive() {
        if (!apply || !pvpAble) {
            return false;
        }
        if (pvpProperty == null) {
            return false;
        }
        if (pvpProperty.getHp() <= 0) {
            return false;
        }
        if (upDownDemand){
            return false;
        }
        long timeMillis = System.currentTimeMillis();
        if (reviveTime > timeMillis) {
            return false;
        }
        if (upDemandTime > 0 && timeMillis < upDemandTime) {
            return false;
        }
        if (downDemandTime > 0 && timeMillis < downDemandTime) {
            return false;
        }
        return true;
    }

    /**
     * 无敌保护时间
     */
    public boolean isProtectTime() {
        return System.currentTimeMillis() <= protectTime;
    }


    /**
     * 获取玩家所在的坐标
     */
    public Coordinate getCoordinate() {
        return new Coordinate(position.getX(), position.getY());
    }

    public int getProfileRadius() {
        return profileRadius;
    }

    public void setProfileRadius(int profileRadius) {
        this.profileRadius = profileRadius;
    }

    public boolean isUpDownDemand() {
        return upDownDemand;
    }

    public void setUpDownDemand(boolean upDownDemand) {
        this.upDownDemand = upDownDemand;
    }

    public long getSpeedEndTime() {
        return speedEndTime;
    }

    public void setSpeedEndTime(long speedEndTime) {
        this.speedEndTime = speedEndTime;
    }

    public long getLastSpeedTime() {
        return lastSpeedTime;
    }

    public void setLastSpeedTime(long lastSpeedTime) {
        this.lastSpeedTime = lastSpeedTime;
    }
}
